package com.xwz.fullstack.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwz.fullstack.entity.LeaveStudy;
import com.xwz.fullstack.entity.Meeting;
import com.xwz.fullstack.entity.MoveStudy;
import com.xwz.fullstack.common.Result;
import com.xwz.fullstack.mapper.MoveStudyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaoWenzhuo
 * @since 2021-11-15
 */
@Slf4j
@RestController
@RequestMapping("/moveStudy")
public class MoveStudyController {
    @Resource
    MoveStudyMapper MoveStudyMapper;

    @PostMapping()
    public Result<?> save(@RequestBody MoveStudy moveStudy) {
        moveStudy.setDateWeek(DateUtil.weekOfYear(DateUtil.date()));
        moveStudy.setDateNow(DateUtil.date());
        LambdaQueryWrapper<MoveStudy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MoveStudy::getNameStudent,moveStudy.getNameStudent());
        wrapper.eq(MoveStudy::getDateWeek,moveStudy.getDateWeek());
        List<MoveStudy> moves = MoveStudyMapper.selectList(wrapper);
        moveStudy.setMoveNumber(String.valueOf(moves.size()+1));
        MoveStudyMapper.insert(moveStudy);
        log.info("研学调整新增------》 " + moveStudy.toString());
        return Result.success();
    }
    @PutMapping
    public Result<?> update(@RequestBody MoveStudy moveStudy) {
        MoveStudyMapper.updateById(moveStudy);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public  Result<?> deleteById(@PathVariable int id) {
        MoveStudyMapper.deleteById(id);
        log.warn("研学调整删除------》deleteById(" + id + ")");
        return Result.success();
    }

    @GetMapping("/findAll")
    public Result<?> findAll() {
        QueryWrapper<MoveStudy> queryWrapper = new QueryWrapper<>();

        List<MoveStudy> list = MoveStudyMapper.selectList(queryWrapper);
        return Result.success(list);
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<MoveStudy> wrapper = Wrappers.<MoveStudy>lambdaQuery();
        wrapper.orderByDesc(MoveStudy::getId);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(MoveStudy::getDateOld, search);
        }
        Page<MoveStudy> userPage = MoveStudyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }


}

